package com.jd.system.controller;


import com.jd.basic.util.AjaxsResult;
import com.jd.basic.util.PageList;
import com.jd.system.domain.Dictionarydetail;
import com.jd.system.query.TypeQuery;
import com.jd.system.service.DetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典",description = "数据字典")
@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private DetailService detailService;


    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "单个查询")
    @GetMapping("/{id}")
    public Dictionarydetail findOne(@PathVariable("id")Long id){
        return detailService.loadById(id);
    }


    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Dictionarydetail> findAll(){
        return detailService.loadAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxsResult remove(@PathVariable("id")Long id){
        try {
            detailService.remove(id);
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"删除失败");
        }
    }

    /**
     * 修改和添加
     */

    @ApiOperation(value = "修改和添加")
    @PutMapping
    public AjaxsResult save(@RequestBody Dictionarydetail dictionarydetail){
        try {
            if (dictionarydetail.getId()==null){
                // 添加
                detailService.save(dictionarydetail);

            }else {
                // 修改
                detailService.update(dictionarydetail);
            }
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"操作失败");
        }
    }

    /**
     * 分页查询
     * PageList<Department>返回参数：需要数据和总条数
     *@PathVariable DepartmentQuery departmentQuery：获取路径的当前页和显示的条数
     */

    @ApiOperation(value = "分页查询")
    @PostMapping // 匹配前端url地址
    public PageList<Dictionarydetail> queryPage(@RequestBody TypeQuery typeQuery){
        return detailService.queryPage(typeQuery);
    }

    // 批量删除
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxsResult patchRemove(@RequestBody List<Long> ids){
        try {
            detailService.patchRemove(ids);
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"删除失败");
        }
    }

}
